package com.itheima.jdbc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcTemplateTest {
    @Test
    public void main(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/itheima/jdbc/application-annotation.xml");
        AccountDao accountDao=(AccountDao) applicationContext.getBean("accountDao");
        accountDao.transfer("Jack","Rose",100.0);
        System.out.println("转账成功！");
    }
public void CreateTable() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/itheima/jdbc/applicationContext.xml");
    JdbcTemplate jdTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
    jdTemplate.execute("create table account(" +
            "id int primary key auto_increment ," +
            "username varchar (50)," +
            "balance double)");
    System.out.println("账户表创建成功！");
}
@Test
public void addData(){
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/itheima/jdbc/application-annotation.xml");
    AccountDao accountDao=(AccountDao) applicationContext.getBean("accountDao");
    Account account=new Account();
    account.setUsername("Chen");
    account.setBanance(2000.00);;
    int num=accountDao.addAccount(account);
    if (num>0){
        System.out.println("成功插入了"+num+"条数据");
    }else {
        System.out.println("插入操作执行失败");
    }
 }
}
